package JUnit;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.After;
import org.junit.BeforeClass;

import SQL.SQLLiteHandler;
import Servlets.WhatsAppConstatnts;

public class mainTest {
	protected String _url = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		//(new Thread(new RunnableServer())).start();
	}

	@After
	public void tearDown() throws Exception {
		String response1 = this.runtest(new RunnableClient("localhost", 9969,
				"GET /" + _url + ".jsp HTTP/1.1\n\n$"));
		SQLLiteHandler sql = new SQLLiteHandler(WhatsAppConstatnts.DBNAME);
		sql.Query("DELETE FROM " + WhatsAppConstatnts.TABLE_GROUPS);
		sql.Query("DELETE FROM " + WhatsAppConstatnts.TABLE_MESSAGES);
		sql.Query("DELETE FROM " + WhatsAppConstatnts.TABLE_USERS);
		sql.Query("DELETE FROM " + WhatsAppConstatnts.TABLE_GROUP_NAMES);
	}

	public String runtest(RunnableClient test) {
		new Thread(test).start();
		while (!test.hasResponse()) {
			try {
				Thread.sleep(30);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return test.getResponse();
	}

	public String getCookie(String response) {
		Pattern regex = Pattern.compile("SESSIONID=.*",
				Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
		Matcher regexMatcher = regex.matcher(response);
		if (regexMatcher.find()) {
			return regexMatcher.group();

		}
		return null;
	}

}
